<template>
    <div id="login">
    <div id="loginBox">
    <el-form :model="loginForm" status-icon :rules="loginRules" ref="ruleForm" label-width="80px" class="demo-ruleForm" id="loginForm">
        <el-form-item label="账号" prop="username">
            <el-input type="text" v-model="loginForm.username" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
            <el-input type="password" v-model="loginForm.password" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item>
            <el-button type="primary" @click="login()">提交</el-button>
            <el-button>重置</el-button>
        </el-form-item>
    </el-form>
    </div>
    </div>
</template>

<script>
   export default {
       name: "Login",
       data(){
           return{
               loginForm:{username:"",password:""},
               //登录规则
               loginRules:{
                   username: [
                       {required: true,message:'请输入账号',trigger:'blur'}
                   ],
                   password: [
                       {required: true,message:'请输入密码',trigger:'blur'}
                   ]
               },
           }
       },
       methods:{
           login(){
                this.$http.post("http://localhost:8080/login",this.loginForm).then(resp=>{
                    if(resp.data.code===2000){
                        //获取服务器响应的token值，保存local Storage
                        localStorage.setItem("token",resp.data.data);
                        //路由跳转
                        this.$router.push("/student")
                    }else {
                        this.$message.error("账号或密码错误")
                    }
                })
           }
       }
   }
</script>

<style scoped>
    #loginBox{
        width: 450px;
        height: 300px;
        border: 1px solid deepskyblue;
        margin: 150px auto;
    }
    #loginForm{
        margin: 20px auto;
    }
</style>